package com.henu.rescue.controller;


import com.henu.rescue.entity.VO.*;
import com.henu.rescue.service.*;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("analysis/")
@Api("统计和分析controller")
@Slf4j
public class Analysis {

    @Autowired
    private StaService staService;

    @Autowired
    private ComplainService complainService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private AccidentService accidentService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping("getStationComplain")
    @ApiOperation("获取每个服务点收到的投诉的数量")
    public Result getStationComplain(){
        List<StationComplainCount> stationComplainCount = staService.getStationComplainCount();
        return new Result(ResultEnums.SUCCESS,stationComplainCount);
    }

    @PostMapping("getComplaintCountByStandard")
    @ApiOperation("获取标准收到的投诉的数量")
    public Result getComplaintCountByStandard(){
        List<StandardNameComplainCount> standardNameComplainCounts = complainService.getComplaintCountByStandard();
        return new Result(ResultEnums.SUCCESS,standardNameComplainCounts);
    }

    @PostMapping("getOrderCount")
    @ApiOperation("获取所有的工单的数量")
    public Result getOrderCount(HttpServletRequest req){
        Integer orderCount = orderService.getOrderCount();
        System.out.println(req.getSession().getAttribute("admin"));
        return new Result(ResultEnums.SUCCESS,orderCount);
    }

    @PostMapping("getComplainCount")
    @ApiOperation("获取所有的投诉的数量")
    public Result getComplainCount(){
        Integer count = complainService.getCount();
        return new Result(ResultEnums.SUCCESS,count);
    }

    @PostMapping("getStationCount")
    @ApiOperation("获取所有救援服务点的数量")
    public Result getStationCount(){
        Integer count = staService.getCount();
        return new Result(ResultEnums.SUCCESS,count);
    }

    @PostMapping("accidentCause")
    @ApiOperation("获取事故原因情况")
    public Result accidentCause(){
        List<AccidentCause> accidentCauses = accidentService.selectAccidentCause();
        return new Result(ResultEnums.SUCCESS,accidentCauses);
    }

    @PostMapping("accTimeCount")
    @ApiOperation("获取时间区间内二十四小时的事故分布数量")
    public Result accTimeCount(@RequestBody Map<String, Date> requestBody){
        Date lowTime = requestBody.get("lowTime");
        Date highTime = requestBody.get("highTime");
        List<AccTimeAndCount> accTimeAndCounts = accidentService.selectAccTime(lowTime, highTime);
        return new Result(ResultEnums.SUCCESS,accTimeAndCounts);
    }

    @PostMapping("accCountYear/{year}")
    @ApiOperation("获取某一年中每个月的事故数量分布情况")
    public Result accCountYear(@PathVariable Integer year){
        List<AccidentYearVO> accidentYearVOS = accidentService.selectAccidentByYear(year);
        return new Result(ResultEnums.SUCCESS,accidentYearVOS);
    }

    @PostMapping("accAllYear")
    @ApiOperation("获取每年事故数量分布情况")
    public Result accAllYear(){
        List<AccidentAllYearVO> accidentAllYearVOS = accidentService.selectAllYear();
        return new Result(ResultEnums.SUCCESS,accidentAllYearVOS);
    }

    @PostMapping("selectNature/{levelId}")
    @ApiOperation("获取事故自然因素占比")
    public Result selectNature(@PathVariable Integer levelId){
        if(levelId > 7){
            return new Result(ResultEnums.PARAMS_ERROR);
        }
        List<CategoryAnalyse> categoryAnalyses = categoryService.selectNatureByLevelId(levelId);
        Iterator<CategoryAnalyse> iterator = categoryAnalyses.iterator();
        while (iterator.hasNext()) {
            CategoryAnalyse categoryAnalyse = iterator.next();
            if (categoryAnalyse.getNum() == 0) {
                iterator.remove();
            }
        }
        return new Result(ResultEnums.SUCCESS,categoryAnalyses);
    }

    @PostMapping("selectHuman")
    @ApiOperation("获取事故人为因素占比")
    public Result selectNature(){
        List<CategoryAnalyse> categoryAnalyses = categoryService.selectHuman();
        Iterator<CategoryAnalyse> iterator = categoryAnalyses.iterator();
        while (iterator.hasNext()) {
            CategoryAnalyse categoryAnalyse = iterator.next();
            if (categoryAnalyse.getNum() == 0) {
                iterator.remove();
            }
        }
        return new Result(ResultEnums.SUCCESS,categoryAnalyses);
    }
}
